//
//  LockViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/9/2.
//
//

#import "LockViewController.h"
#import "HomeViewController.h"
#import "MineViewController.h"

@import LocalAuthentication;

@interface LockViewController ()
@property (nonatomic, strong) UIButton *loginBtn;
@end

@implementation LockViewController
#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loginBtn];
    [self unlock:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 方法 methods
- (void)unlock:sender {
    LAContext *laContext = [[LAContext alloc] init];
    NSError *err;
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&err]) {
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"通过Home键验证已有手机指纹" reply:^(BOOL success, NSError *error) {
            if (success) {
                [self enterMainScreen];
            }
            if (error) {
                if (error.code == kLAErrorUserFallback) {
                    NSLog(@"User tapped Enter Password");
                } else if (error.code == kLAErrorUserCancel) {
                    NSLog(@"User tapped Cancel");
                } else {
                    NSLog(@"Authenticated failed.");
                }
            }
        }];
    } else {
        NSLog(@"该设备不支持指纹识别 error:%@", err.description);
        [self.view showWarning:@"您的设备不支持指纹识别"];
    }
}

- (void)enterMainScreen {
    HomeViewController *homeVC = [HomeViewController new];
    UINavigationController *homeNAC = [[UINavigationController alloc]initWithRootViewController:homeVC];
    homeNAC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:nil selectedImage:nil];
    
    MineViewController *mineVC = [[MineViewController alloc]init];
    UINavigationController *mineNAC = [[UINavigationController alloc]initWithRootViewController:mineVC];
    mineNAC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:nil selectedImage:nil];
    
    UITabBarController *tabbar = [[UITabBarController alloc]init];
    tabbar.viewControllers = [NSArray arrayWithObjects:homeNAC,mineNAC, nil];
    tabbar.tabBar.tintColor = HS;
    
    [self presentViewController:tabbar animated:YES completion:nil];
}

#pragma mark - 懒加载 lazy loading
- (UIButton *)loginBtn {
	if(_loginBtn == nil) {
		_loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_loginBtn];
        [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(0);
        }];
        _loginBtn.backgroundColor = [UIColor whiteColor];
        [_loginBtn setTitle:@"指纹登陆" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(unlock:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _loginBtn;
}

@end
